const app = getApp()
const { processListImages } = require('../../utils/image')
const request = require('../../utils/request.js');
Page({
  data: {
    currentStatus: '',
    demandList: [],
    page: 1,
    pageSize: 10,
    hasMore: true,
    loading: false
  },

  onLoad() {
    this.loadDemandList()
  },

  onShow() {
    if(app.globalData.needRefresh) {
      this.setData({
        currentStatus: '',
        demandList: [],
        page: 1,
        hasMore: true
      }, () => {
        this.loadDemandList()
      })
      app.globalData.needRefresh = false
    }
  },

  // 加载需求列表
  loadDemandList() {
    if (this.data.loading || !this.data.hasMore) return
    this.setData({ loading: true })
    // 调用后端API获取发布列表
    return new Promise((resolve, reject) => {
      // 构建请求参数
      const params = {
        page: this.data.page,
        pageSize: this.data.pageSize,
        status: this.data.currentStatus
      }
      request.request({
        url: `${app.globalData.baseUrl}/api/demands/user/demands`,
        method: 'GET',
        data: params,
        header: {
          'Authorization': `Bearer ${app.getGlobalToken()}`
        },
        success: (res) => {
          const { list, total } = res.data.data

          // 使用工具函数处理图片URL
          const processedList = processListImages(list)

          this.setData({
            demandList: [...this.data.demandList, ...processedList],
            hasMore: this.data.demandList.length + processedList.length < total,
            loading: false
          })
          resolve()
        },
        fail: reject
      })
    })
  },

  // 切换状态标签
  switchStatus(e) {
    const status = e.currentTarget.dataset.status
    if (status === this.data.currentStatus) return

    this.setData({
      currentStatus: status,
      demandList: [],
      page: 1,
      hasMore: true
    }, () => {
      this.loadDemandList()
    })
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.setData({
      demandList: [],
      page: 1,
      hasMore: true
    }, () => {
      this.loadDemandList()
      wx.stopPullDownRefresh()
    })
  },

  // 上拉加载更多
  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    }, () => {
      this.loadDemandList()
    })
  },

  // 编辑需求
  editDemand(e) {
    const demandId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/demand/publish/publish?id=${demandId}`
    })
  },

  // 取消需求
  cancelDemand(e) {
    const demandId = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '确定要下架该需求吗？',
      success: (res) => {
        if (res.confirm) {
          // TODO: 调用后端API取消需求
          request.request({
            url: `${app.globalData.baseUrl}/api/demands/${demandId}/cancel`,
            method: 'POST',
            header: {
              'Authorization': `Bearer ${app.getGlobalToken()}`
            },
            success: () => {
              wx.showToast({
                title: '需求已下架',
                icon: 'success'
              })
              this.setData({
                demandList: [],
                page: 1,
                hasMore: true
              })
              this.loadDemandList()
            }
          })
        }
      }
    })
  },

  // 删除需求
  deleteDemand(e) {
    const demandId = e.currentTarget.dataset.id
    wx.showModal({
      title: '提示',
      content: '确定要删除该需求吗？',
      success: (res) => {
        if (res.confirm) {
          // TODO: 调用后端API删除需求
          request.request({
            url: `${app.globalData.baseUrl}/api/demands/${demandId}`,
            method: 'delete',
            header: {
              'Authorization': `Bearer ${app.getGlobalToken()}`
            },
            success: () => {
              wx.showToast({
                title: '已删除',
                icon: 'success'
              })
              this.setData({
                postList: [],
                page: 1,
                hasMore: true
              })
              this.loadDemandList()
            }
          })
        }
      }
    })
  },

  // 跳转到详情页
  goToDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/demand/detail/detail?id=${id}`
    })
  },

  // 发布新需求
  goToPublish() {
    wx.navigateTo({
      url: '/pages/demand/publish/publish'
    })
  }
}) 